package org.project.single.controller.back;

import org.project.single.pojo.ro.EnableRo;
import org.project.single.pojo.ro.user.EditBackUserRo;
import org.project.single.pojo.systemEnum.SystemCodeEnum;
import org.project.single.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.stone.project.baseCommon.pojo.dto.CommonSingle;
import org.stone.project.baseCommon.pojo.vo.R;
import org.stone.project.baseCommon.service.Insert;
import org.stone.project.baseCommon.service.Update;
import org.stone.project.baseCommon.sysException.BuzException;

import javax.validation.Valid;

/**
 * 用户后台接口
 */
@RestController
@RequestMapping("/back/user")
public class SysUserBackController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 添加用户
     * @return
     */
    @PostMapping("/addUser")
    public R addUser(@RequestBody @Validated(Insert.class) EditBackUserRo editBackUserRo){
        if (!editBackUserRo.getPassword().equals(editBackUserRo.getPasswordConfirm()))
            throw new BuzException(SystemCodeEnum.DATA_ERR_CODE.getCode(), "密码不一致");
        sysUserService.editBackUser(editBackUserRo);
        return R.ok();
    }

    /**
     * 修改用户
     * @return
     */
    @PutMapping("/updateUser")
    public R updateUser(@RequestBody @Validated(Update.class) EditBackUserRo editBackUserRo){
        editBackUserRo.setUsername(null);
        editBackUserRo.setPassword(null);
        editBackUserRo.setPasswordConfirm(null);
        sysUserService.editBackUser(editBackUserRo);
        return R.ok();
    }

    /**
     * 用户启用
     * @param enableRo
     * @return
     */
    @PostMapping("enableUser")
    public R enableUser(@RequestBody @Valid EnableRo enableRo){
        sysUserService.enableUser(enableRo);
        return R.ok();
    }

    /**
     * 用户解锁
     * @return
     */
    @PostMapping("unLockUser")
    public R unLockUser(@RequestBody @Valid CommonSingle<String> commonSingle){
        sysUserService.unLockUser(commonSingle.getValue());
        return R.ok();
    }
}
